package offer;

import java.util.LinkedList;
import java.util.List;

public class offerII_0015 {
    public List<Integer> findAnagrams(String s, String p) {
        int len1=p.length();
        int len2=s.length();
        List<Integer> list=new LinkedList<>();
        if(len2<len1) return list;
        int[] num=new int[26];
        for(int i=0;i<len1;++i){
            num[p.charAt(i)-'a']+=1;
            num[s.charAt(i)-'a']-=1;

        }
        if(empty(num)) list.add(0);
        int left=0;
        int right=len1;
        while(right<len2){
            num[s.charAt(left)-'a']+=1;
            left++;
            num[s.charAt(right)-'a']-=1;
            right++;
            if(empty(num)) list.add(left);
        }
        return list;
    }
    public boolean empty(int[] num){
        for(int i=0;i<26;++i){
            if(num[i]!=0) return false;
        }
        return true;
    }
}
